﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using JiveMessenger.User;
using GalaSoft.MvvmLight.Messaging;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System;
using Microsoft.Win32;
using System.ComponentModel;
using JiveMessenger.Utilities;
using NLog;
using System.IO;

namespace JiveMessenger.Views
{
    /// <summary>
    /// Interaction logic for PersonalPage.xaml
    /// </summary>
    public partial class PersonalPage : Page, INotifyPropertyChanged
    {
        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
        private const int _maxAvatarByteSize = 512000;
        private IClient _currentClient;

        private string _avatarImageErrorText;
        public string AvatarImageErrorText
        {
            get { return _avatarImageErrorText; }
            set
            {
                if (_avatarImageErrorText != value)
                {
                    _avatarImageErrorText = value;
                    RaisePropertyChanged("AvatarImageErrorText");
                }
            }
        }

        public FileInfo AvatarFile { get; set; }

        private byte[] _imageSource;
        public byte[] AvatarImageSource
        {
            get { return _imageSource; }
            set
            {
                if (_imageSource != value)
                {
                    _imageSource = value;
                    RaisePropertyChanged("AvatarImageSource");
                }
            }
        }



        public PersonalPage(IClient currentClient)
        {
            InitializeComponent();
            this.LayoutRoot.DataContext = this;
            _currentClient = currentClient;
            this.txtBoxNickname.Text = _currentClient.UserSettings.NickName;
            if (_currentClient.UserSettings.Avatar != null && !String.IsNullOrEmpty(_currentClient.UserSettings.Avatar.Hash))
            {
                try
                {
                    AvatarFile = new FileInfo(StaticMethods.CombinePath(ProgramSettings.Instance.AvatarFolder, currentClient.CurrentUserInfo.UserName, currentClient.UserSettings.Avatar.Hash + currentClient.UserSettings.Avatar.Extension));
                    AvatarImageSource = StaticMethods.LoadImageData(AvatarFile.FullName);
                }
                catch (Exception e)
                {
                    MessageBox.Show("Error opening avatar image: " + e.Message);
                    Logger.ErrorException("Error opening avatar image", e);
                }
            }
        }

        private void ButtonDeleteCustomEmoticons_Click(object sender, RoutedEventArgs e)
        {
            const MessageBoxButton btnMessageBox = MessageBoxButton.YesNo;
            const MessageBoxImage icnMessageBox = MessageBoxImage.Warning;
            MessageBoxResult rsltMessageBox = MessageBox.Show("Do you want to reset your custom emoticon list?", "Reset emoticons", btnMessageBox, icnMessageBox);
            switch (rsltMessageBox)
            {
                case MessageBoxResult.Yes:
                    {
                        _currentClient.UserSettings.CustomEmoticons.Clear();
                        break;
                    }
                case MessageBoxResult.No:
                    break;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Messenger.Default.Send<NotificationMessage>(new NotificationMessage(this, "ChangeFont"));
        }

        private void BnSelectAvatar_Click(object sender, RoutedEventArgs e)
        {
            var fd = new Microsoft.Win32.OpenFileDialog
            {
                Filter = "Image Files(*.BMP;*.JPG;*.GIF;*.PNG;*.TIFF;)|*.BMP;*.JPG;*.GIF;*.PNG;*.TIFF"
            };

            if (fd.ShowDialog().GetValueOrDefault() == true)
            {
                AvatarFile = new FileInfo(fd.FileName);

                byte[] imgSource = AvatarFile.ToByteArray();
                if (imgSource == null)
                    AvatarImageErrorText = "File is not a valid image";
                else if (imgSource.Length > _maxAvatarByteSize)
                    AvatarImageErrorText = string.Format("File size is too large. Avatar can be at most {0}KB", _maxAvatarByteSize / 1000);
                else
                {
                    AvatarImageSource = imgSource;
                    AvatarImageErrorText = string.Empty;
                }
            }
        }
        private void BnRemoveAvatar_Click(object sender, RoutedEventArgs e)
        {
            this.AvatarImageSource = null;
            this.AvatarFile = null;
        }
        protected virtual void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;


    }
}

